package com.lee.xcore.utils

import android.app.Activity
import android.os.Process
import java.util.*
import kotlin.system.exitProcess

/**
 * Activity堆栈管理工具类
 */
class XStackUtil {
    val TAG = "Activity堆栈管理"

    companion object {
        val instance: XStackUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            XStackUtil()
        }
    }

    var mActivityStack: Stack<Activity>?=null

    fun getStack(): Stack<Activity>? {
        if (null == mActivityStack) {
            mActivityStack = Stack<Activity>()
        }
        return mActivityStack
    }

    fun getCurrent(): Activity? {
        return if (mActivityStack != null && mActivityStack!!.size != 0) {
            val activity: Activity =
                mActivityStack!!.lastElement()
            activity
        } else {
            null
        }
    }

    fun getPrevious(): Activity? {
        return if (mActivityStack!!.size >= 2) {
            val activity: Activity =
                mActivityStack!!.get(mActivityStack!!.size - 2)
            activity
        } else {
            null
        }
    }

    fun getActivity(cls: Class<*>?): Activity? {
        if (mActivityStack == null || mActivityStack!!.size == 0 || cls == null) {
            return null
        }
        for (activity in mActivityStack!!) {
            if (activity != null && activity.javaClass == cls) {
                return activity
            }
        }
        return null
    }

    fun push(activity: Activity): XStackUtil {
        if (mActivityStack == null) {
            mActivityStack = Stack()
        }
        mActivityStack!!.add(activity)
        return instance
    }

    fun pop(activity: Activity?): XStackUtil {
        return pop(activity, true)
    }

    fun pop(
        activity: Activity?,
        isFinish: Boolean
    ): XStackUtil {
        if (activity != null) {
            if (mActivityStack!!.contains(activity)) {
                mActivityStack!!.remove(activity)
            }
            if (isFinish) {
                activity.finish()
            }
        }
        return instance
    }

    fun popAll(): XStackUtil {
        while (mActivityStack!!.size > 0) {
            val activity = getCurrent() ?: break
            pop(activity)
        }
        return instance
    }

    fun popAllExceptCurrent(cls: Class<*>): XStackUtil {
        while (true) {
            val activity = getCurrent()
            if (activity == null || activity.javaClass == cls) {
                return instance
            }
            pop(activity)
        }
    }

    fun popAllExceptCurrent(): XStackUtil {
        while (true) {
            val activity =
                getPrevious() ?: return instance
            pop(activity)
        }
    }

    fun exit(): XStackUtil {
        return exit(true)
    }

    fun exit(kill: Boolean): XStackUtil {
        try {
            popAll()
            if (kill) {
                //退出JVM(java虚拟机),释放所占内存资源,0表示正常退出(非0的都为异常退出)
                exitProcess(0)
                //从操作系统中结束掉当前程序的进程
                Process.killProcess(Process.myPid())
            }
        } catch (e: Exception) {
            exitProcess(-1)
        }
        return instance
    }

}